package com.qf.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsPermissionMapper;
import com.qf.mapper.DtsRoleMapper;
import com.qf.pojo.DtsPermission;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsRoleService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class DtsRoleServiceImpl implements DtsRoleService {
    @Autowired
    private DtsRoleMapper dtsRoleMapper;
    @Autowired
    private DtsPermissionMapper dtsPermissionMapper;
    @Override
    public Set<String> findRolesByIds(Integer[] roleIds) {
        // select name from dts_role where id in  (1,2,3)
        List<DtsRole> dtsRoleList = dtsRoleMapper.selectList(new LambdaQueryWrapper<DtsRole>()
                .select(DtsRole::getName)
                .in(DtsRole::getId, roleIds)
                .eq(DtsRole::getDeleted,false));
        Set<String> roles = dtsRoleList.stream().map(DtsRole::  getName).collect(Collectors.toSet());
        return roles;
    }

    /**
     * 查询所有的角色
     * @return
     */
    @Override
    public List<Map<String,Object>> options() {
        QueryWrapper<DtsRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id value ,name label").eq("deleted",false);//启用
        return dtsRoleMapper.selectMaps(queryWrapper);

    }
    /**
     * 查询角色 分页
     * @param p
     * @param limit
     * @param sort
     * @param order
     * @param rolename
     * @return
     */
    @Override
    public IPage<DtsRole> list(Integer p, Integer limit, String sort, String order, String rolename) {
        //MybatisPlus相当了解
        return dtsRoleMapper.selectPage(new Page<DtsRole>(p,limit),new QueryWrapper<DtsRole>()
                .like(!StringUtils.isEmpty(rolename),"`name`",rolename)
                .orderBy(true,"asc".equalsIgnoreCase(order),sort));
    }

    /**
     * 角色管理 更新角色的权限
     * @param permissions
     * @param roleId
     */
    @Override
    public void updatePerms(List<String> permissions, Integer roleId) {

        DtsPermission dtsPermission = new DtsPermission();
        dtsPermission.setDeleted(true);//逻辑删除  假删除

        dtsPermissionMapper.update(dtsPermission,new QueryWrapper<DtsPermission>()
                .eq("role_id",roleId));
        //偷天换日
        List<DtsPermission> dtsPermissionList = permissions.stream().map(perm -> {
            DtsPermission dtsPermission1 = new DtsPermission();
            dtsPermission1.setDeleted(false);
            dtsPermission1.setPermission(perm);
            dtsPermission1.setRoleId(roleId);
            dtsPermission1.setUpdateTime(new Date());
            dtsPermission1.setAddTime(new Date());
            return dtsPermission1;
        }).collect(Collectors.toList());

        //批量添加  insert into dts_permission (...)  values (),(),(),(),()........
        dtsPermissionMapper.insertPerms(dtsPermissionList);


    }
}
